package com.screen.screendisplay.service.impl.coal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.screen.screendisplay.entity.coal.CoalProdoperSalesStructure;
import com.screen.screendisplay.mapper.coal.CoalProdoperSalesStructureMapper;
import com.screen.screendisplay.service.coal.CoalProdoperSalesStructureService;
import com.screen.screendisplay.vo.coal.CoalSalesStructureVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 生产经营煤炭销售结构表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalProdoperSalesStructureServiceImpl extends ServiceImpl<CoalProdoperSalesStructureMapper, CoalProdoperSalesStructure> implements CoalProdoperSalesStructureService {

    @Override
    public CoalSalesStructureVO getSalesStructure(Integer year) {
        // 如果未指定年份，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }
        
        // 查询指定年份的数据
        LambdaQueryWrapper<CoalProdoperSalesStructure> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CoalProdoperSalesStructure::getYear, year);
        
        List<CoalProdoperSalesStructure> structureList = this.list(wrapper);
        
        // 初始化VO
        CoalSalesStructureVO vo = new CoalSalesStructureVO();
        BigDecimal longTermSales = BigDecimal.ZERO;  // 长协煤销售量
        BigDecimal marketSales = BigDecimal.ZERO;    // 市场煤销售量
        BigDecimal internalSales = BigDecimal.ZERO;  // 系统内销售量
        BigDecimal externalSales = BigDecimal.ZERO;  // 系统外销售量
        
        // 遍历数据进行聚合统计
        for (CoalProdoperSalesStructure structure : structureList) {
            // 将吨转换为万吨（除以10000）
            BigDecimal volumeInWanTon = structure.getSalesVolume()
                    .divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP);
            
            // 按合同类型统计
            if (structure.getContractType() != null && structure.getContractType() == 0) {
                // 长协煤
                longTermSales = longTermSales.add(volumeInWanTon);
            } else if (structure.getContractType() != null && structure.getContractType() == 1) {
                // 市场煤
                marketSales = marketSales.add(volumeInWanTon);
            }
            
            // 按销售渠道统计
            if (structure.getSalesChannel() != null && structure.getSalesChannel() == 0) {
                // 系统内
                internalSales = internalSales.add(volumeInWanTon);
            } else if (structure.getSalesChannel() != null && structure.getSalesChannel() == 1) {
                // 系统外
                externalSales = externalSales.add(volumeInWanTon);
            }
        }
        
        vo.setLongTermSales(longTermSales);
        vo.setMarketSales(marketSales);
        vo.setInternalSales(internalSales);
        vo.setExternalSales(externalSales);
        
        return vo;
    }
}
